package org.cos.verification.verifier;

import java.io.IOException;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;

import org.shellex.javame.utility.FileBrowser;
import org.shellex.javame.utility.Screen;

public class OpenFileScreen extends Screen {

	public FileBrowser screen;
	private Command cancelFileBrowserCmd;

	private static OpenFileScreen instance;
	public static OpenFileScreen getInstance()
	{
		if(instance == null)
			instance = new OpenFileScreen();
		return instance;
	} 

	public void crackCommand(MIDlet midlet, Command cmd, Displayable screen) {
		if (cmd == FileBrowser.SELECT_FILE_COMMAND) {
			SharePool.getInstance().crr_filename = this.screen.getSelectedFileURL();
			try {
				SharePool.getInstance().crr_filepath = this.screen.getSelectedFile().getName();
			} catch (IOException e) {
				e.printStackTrace();
			}
			CheckProgressScreen.getInstance().display(midlet);
		} else if (cmd == cancelFileBrowserCmd) {
			MainScreen.getInstance().display(midlet);
		}
	}

	public Screen initCommand() {
		cancelFileBrowserCmd = new Command("取消", Command.CANCEL, 1);
		screen.addCommand(FileBrowser.SELECT_FILE_COMMAND);
		screen.addCommand(cancelFileBrowserCmd);
		return this;
	}

	public Screen initUI(MIDlet midlet) {
		screen = new FileBrowser(Display.getDisplay(midlet));                                 
		screen.setTitle("Open a file");
		return this;
	}

	public void setCommandListener(CommandListener cmdl) {
		screen.setCommandListener(cmdl);

	}
	public void display(MIDlet midlet) {
		Display.getDisplay(midlet).setCurrent(screen);
	}
}
